//
//  BIDReadConditionView.m
//  shangwuting
//
//  Created by mal on 14-2-20.
//  Copyright (c) 2014年 mal. All rights reserved.
//

#import "BIDReadConditionView.h"
#define LINE_SPACING 10
@implementation BIDReadConditionView
@synthesize titlesArr;
@synthesize contentArr;

- (id)initWithFrame:(CGRect)frame titlesArr:(NSArray *)arr1 contentArr:(NSArray *)arr2
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        titlesArr = [[NSArray alloc] initWithArray:arr1];
        contentArr = [[NSArray alloc] initWithArray:arr2];
        [self initView];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)initView
{
    self.layer.borderWidth = 1;
    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.layer.cornerRadius = 5;
    [self setBackgroundColor:[UIColor colorWithRed:230.0f/255.0f green:230.0f/255.0f blue:230.0f/255.0f alpha:1.0f]];
    //
    int topSpacing = 15;
    int bottomSpacing = 15;
    int leftSpacing = 0;
    int itemSpacing = 8;
    int rightSpacing = 2;
    int labelWidth1 = 72;
    int labelHeight = 21;
    UIFont *font1 = [UIFont systemFontOfSize:13.0f];
    UIFont *font2 = [UIFont systemFontOfSize:15.0f];
    NSMutableArray *lineArr = [[NSMutableArray alloc] initWithCapacity:titlesArr.count-1];
    NSMutableArray *labelArr = [[NSMutableArray alloc] init];

    for(int i=0; i<titlesArr.count; i++)
    {
        CGSize labelSize1 = CGSizeMake(0, 0);
        CGSize labelSize2 = CGSizeMake(0, 0);
        NSString *strContent = contentArr[i];

        CGRect labelFrame1 = CGRectMake(0, 0, 0, 0);
        CGRect labelFrame2 = CGRectMake(0, 0, 0, 0);
        
        //根据文本内容获取label需要的宽度和高度
        NSString *strTitle = [titlesArr objectAtIndex:i];
        labelSize1 = [strTitle sizeWithFont:font1 constrainedToSize:CGSizeMake(MAXFLOAT, labelHeight) lineBreakMode:NSLineBreakByWordWrapping];
        labelSize1.width = labelSize1.width>labelWidth1?labelSize1.width:labelWidth1;
        labelSize1.height = labelHeight;
        if(i==0)
        {
            labelFrame1 = CGRectMake(leftSpacing, topSpacing, labelSize1.width, labelSize1.height);
        }
        else
        {
            UILabel *line = (UILabel*)[lineArr objectAtIndex:i-1];
            labelFrame1 = CGRectMake(leftSpacing, line.frame.origin.y+line.frame.size.height+topSpacing, labelSize1.width, labelSize1.height);
        }
        //标题label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:labelFrame1];
        titleLabel.text = strTitle;
        titleLabel.font = font1;
        titleLabel.textColor = [UIColor colorWithRed:0 green:153.0f/255.0f blue:247.0f/255.0f alpha:1.0f];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        //内容label
        labelFrame2.origin.x = labelFrame1.origin.x+labelFrame1.size.width+itemSpacing;
        labelFrame2.origin.y = labelFrame1.origin.y;
        labelFrame2.size.width = self.frame.size.width-rightSpacing-(labelFrame1.origin.x+labelFrame1.size.width)-itemSpacing;
        labelSize2 = [strContent sizeWithFont:font2 constrainedToSize:CGSizeMake(labelFrame2.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
        //ios7下得到的高度有偏差，所以要加1
        labelSize2.height += 1;
        labelFrame2.size.height = labelSize2.height>labelHeight?labelSize2.height:labelHeight;
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:labelFrame2];
        //
        NSMutableAttributedString *strAttributeContent = [[NSMutableAttributedString alloc] initWithString:strContent];
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        [paraStyle setLineSpacing:10];
        [strAttributeContent addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [strContent length])];
        //
        contentLabel.attributedText = strAttributeContent;
        //contentLabel.text = strContent;
        contentLabel.font = font2;
        contentLabel.textColor = [UIColor blackColor];
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.numberOfLines = 0;
        [contentLabel setBackgroundColor:[UIColor clearColor]];
        if(strContent.length==0)
        {
        }
        else
        {
            [contentLabel sizeToFit];
        }
        
        //分界线
        CGRect lineFrame = CGRectMake(0, labelFrame2.origin.y+labelFrame2.size.height+bottomSpacing, self.frame.size.width, 1);
        UILabel *lineLabel = [[UILabel alloc] initWithFrame:lineFrame];
        [lineLabel setBackgroundColor:[UIColor lightGrayColor]];
        //
        [self addSubview:titleLabel];
        [self addSubview:contentLabel];
        [labelArr addObject:contentLabel];

        if(i==titlesArr.count-1)
        {}
        else
        {
            [self addSubview:lineLabel];
            [lineArr addObject:lineLabel];
        }

    }//for
    
    UILabel *temporaryLabel = (UILabel*)[labelArr objectAtIndex:labelArr.count-1];
    CGRect ownFrame = self.frame;
    ownFrame.size.height = temporaryLabel.frame.origin.y + temporaryLabel.frame.size.height + bottomSpacing;
    self.frame = ownFrame;
}

@end
